package cn.com.generaldata.os.common;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.com.generaldata.os.service.common.UserService;
import cn.com.generaldata.os.vo.UserVO;

public class UserFilter implements Filter {

	private UserService userService;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(filterConfig
				.getServletContext());
		userService = wac.getBean(UserService.class);
	}

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		String username = "";
		AttributePrincipal principal = null;
		
		if (req.getUserPrincipal()!=null){
			username =  req.getUserPrincipal().getName();
			principal = (AttributePrincipal) req.getUserPrincipal(); 
			String[] moduleName = (URLDecoder.decode((String)principal.getAttributes().get("moduleNameStr"), "utf-8")).split(",");
			String moduleNameStr = "";
			String enableStr = (String)principal.getAttributes().get("enableStr");
			
			if (enableStr.length()>0){
				for (int i=0; i<enableStr.length(); i++){
					if ("1".equals(enableStr.substring(i, i+1))){
						moduleNameStr = moduleNameStr + "," + moduleName[i];
					}
				}
			}
			
			req.getSession().setAttribute("moduleNameStr", moduleNameStr);
		}

		if (req.getSession().getAttribute("user") == null&&!"".equals(username)) {

			
			// 取出用户
			UserVO voUser = userService.toVo(userService.loginNew(username));
			if (voUser == null) {
				request.setAttribute("msg", "用户不存在。");
				throw new RuntimeException("用户不存在。");
			}

			// 保存到session
			req.getSession().setAttribute("user", voUser);

			// 单点登录，提供了username和accessMap，放在了session中
			// 这里得模拟一下。
			req.getSession().setAttribute("username", username);

			// 根据角色ID处理
			//			String roleId = voUser.getRoleId();
			//			if (roleId.equals("1")) {
			//				// 系统管理员
			//				res.sendRedirect(req.getContextPath() + "/static/admin/main.jsp");
			//			} else if (roleId.equals("3") || roleId.equals("5")) {
			//				// 企业用户，涉海部分禁止内网
			//				res.sendRedirect(req.getContextPath() + "/static/company/main.jsp");
			//			} else if (roleId.equals("4")) {
			//				// 省区局用户，或者叫海洋局用户
			//				res.sendRedirect(req.getContextPath() + "/mainTitle/main");
			//			}
		}

		chain.doFilter(request, response);
	}

}